﻿using Super.Admin.Entities;
using Super.Admin.Models.Announcement;

namespace Super.Admin.Mappers
{
    /// <summary>
    /// AnnouncementMapper 类用于在 AnnouncementEntity 和 AnnouncementModel 之间进行转换。
    /// </summary>
    public static class AnnouncementMapper
    {
        /// <summary>
        /// 将 AnnouncementEntity 转换为 AnnouncementBaseModel。
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static AnnouncementBaseModel ToBaseModel(this AnnouncementEntity entity)
        {
            if (entity == null) return null;
            return new AnnouncementBaseModel
            {
                Id = entity.Id,
                Title = entity.Title,
                Content = entity.Content,
                CreateTime = entity.CreateTime,
                UpdateTime = entity.UpdateTime,
                CreateUserId = entity.CreateUserId,
                UpdateUserId = entity.UpdateUserId,
                Status = entity.Status,
                PublishTime = entity.PublishTime,
                PublishUserId = entity.PublishUserId,
            };
        }
        /// <summary>
        /// 将 AnnouncementEntity 转换为 AnnouncementDetailModel。
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static AnnouncementDetailModel ToDetailModel(this AnnouncementEntity entity)
        {
            if (entity == null) return null;
            return new AnnouncementDetailModel
            {
                Id = entity.Id,
                Title = entity.Title,
                Content = entity.Content,
                CreateTime = entity.CreateTime,
                UpdateTime = entity.UpdateTime,
                CreateUserId = entity.CreateUserId,
                UpdateUserId = entity.UpdateUserId,
                Status = entity.Status,
                PublishTime = entity.PublishTime,
                PublishUserId = entity.PublishUserId,
                CreateUser = entity.CreateUser?.ToBaseModel(),
                UpdateUser = entity.UpdateUser?.ToBaseModel(),
                PublishUser = entity.PublishUser?.ToBaseModel()
            };
        }
        /// <summary>
        /// 将 AnnouncementBaseModel 转换为 AnnouncementEntity。
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static AnnouncementEntity ToEntity(this AnnouncementBaseModel model)
        {
            if (model == null) return null;
            return new AnnouncementEntity
            {
                Id = model.Id,
                Title = model.Title,
                Content = model.Content,
                CreateTime = model.CreateTime,
                UpdateTime = model.UpdateTime,
                CreateUserId = model.CreateUserId,
                UpdateUserId = model.UpdateUserId,
                Status = model.Status,
                PublishTime = model.PublishTime,
                PublishUserId = model.PublishUserId,
            };
        }
        /// <summary>
        /// 将 AnnouncementDetailModel 转换为 AnnouncementEntity。
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static AnnouncementEntity ToEntity(this AnnouncementDetailModel model)
        {
            if (model == null) return null;
            return new AnnouncementEntity
            {
                Id = model.Id,
                Title = model.Title,
                Content = model.Content,
                CreateTime = model.CreateTime,
                UpdateTime = model.UpdateTime,
                CreateUserId = model.CreateUserId,
                UpdateUserId = model.UpdateUserId,
                Status = model.Status,
                PublishTime = model.PublishTime,
                PublishUserId = model.PublishUserId,
            };
        }
        /// <summary>
        /// 将 AnnouncementEntity 列表转换为 AnnouncementBaseModel 列表。
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<AnnouncementBaseModel> ToBaseModels(this IEnumerable<AnnouncementEntity> entities)
        {
            return entities?.Select(e => e.ToBaseModel()).ToList() ?? new List<AnnouncementBaseModel>();
        }
        /// <summary>
        /// 将 AnnouncementEntity 列表转换为 AnnouncementDetailModel 列表。
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<AnnouncementDetailModel> ToDetailModels(this IEnumerable<AnnouncementEntity> entities)
        {
            return entities?.Select(e => e.ToDetailModel()).ToList() ?? new List<AnnouncementDetailModel>();
        }
        /// <summary>
        /// 将 AnnouncementBaseModel 列表转换为 AnnouncementEntity 列表。
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public static IEnumerable<AnnouncementEntity> ToEntities(this IEnumerable<AnnouncementBaseModel> models)
        {
            return models?.Select(m => m.ToEntity()).ToList() ?? new List<AnnouncementEntity>();
        }
    }
}
